// 请求 url - > html（信息）  -> 解析html
const https = require('https');
const cheerio = require('cheerio');
const fs = require('fs');
const request =require('request')
const iconv = require('iconv-lite');
const baseUrl ='https://www.17jita.com/tab/img/5375.html'
// const baseUrl ='https://www.17jita.com/tab/img/5390.html'
let body=''
const fims =require('./filmss.json')
new Promise((resolve, reject) => {
  

 

})
let nxtUrl =(url)=>{
    let titles =''
    let pics =''
    request ({url: url,
        encoding: null,
        'User-Agent': 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
        gzip:true
      }, async (err, res, bodys) => {
          body = iconv.decode(bodys, 'gb2312');
          const $ = cheerio.load(body)
          $('#article_contents').find('a')
          $('#article_contents').find('a').each(function(){
            const title = $(this).text();
            const pic = $(this).attr('href');
          titles =title
          pics =pic
          console.log(title,pic);

          })
      })
     
      setTimeout(()=>{
          // console.log(titles,pics);
          return {titles,pics}
      },500)
}
const nxtFn=()=>{
    let allFilms = [];
    let aDom = [];
    fims.forEach((item,index) =>{
      if( index===25) return
        request ({url: item.url,
            encoding: null,
            'User-Agent': 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
            gzip:true
          }, async (err, res, bodys) => {
              body = iconv.decode(bodys, 'gb2312');
              const $ = cheerio.load(body)
         const a =  $('#article_content')
     
             a.each(function(){
                $(this).find('a').each(function(){
                    if($(this).attr('href').match('.html')){
                        `https://www.17jita.com/${$(this).attr('href')}`
                        aDom.push({title:$(this).text(),url: `https://www.17jita.com/${$(this).attr('href')}`})
                    }
                  
                })
               
                aDom.forEach(item =>{
                  nxtUrl(item.url)
                   console.log( );
                })

            
                // console.log(url,title);
                // console.log(  nxtUrl(url));
               
            })
         

          })
         
    })
    setTimeout(()=>{
        // console.log(allFilms);
    },500)
}

module.exports = nxtFn
